package core;

import java.util.Vector;
import javax.microedition.rms.*;

public class MessageManager
{
	private Vector messages;
	private String rmsName;
	
	public MessageManager()
	{
		messages = new Vector();
		rmsName = "messages";
		load();
	}
	
	private void load()
	{
		clear();
		byte[] temp;
		try
		{
			RecordStore recordStore = RecordStore.openRecordStore(rmsName, true);
			RecordEnumeration re = recordStore.enumerateRecords(null, null, false);
			if (re.numRecords()!=0)
			{
				re.rebuild();
				while (re.hasNextElement())
				{
					int id = re.nextRecordId();
					try
					{
						temp = recordStore.getRecord(id);
						if (temp[0]==2)
						{
							messages.insertElementAt((new Message(temp)),0);
						}
					}catch(Exception e){}
				}
			}
			recordStore.closeRecordStore();
			re.destroy();
		}catch(RecordStoreException rse){};

	}
	
	public void save()
	{
            //Удаляем все заказы,чтоб не дублировать данные
            try
            {
                 RecordStore.deleteRecordStore(rmsName);
            }catch(Exception e){}
            
            //��������� ������
            byte[] res;
            try
            {
                    RecordStore recordStore = RecordStore.openRecordStore(rmsName, true);
                    for(int i=0;i<messages.size();i++)
                    {
                            res = ((Message)messages.elementAt(i)).toByte();
                            recordStore.addRecord(res,0,res.length);
                    }
                    recordStore.closeRecordStore();
            }catch(RecordStoreException rse){};
	}
	
	public void clear()
	{
		messages.removeAllElements();
	}

        public void clear(int orderId)
        {
             for (int i=messages.size()-1;i>=0;--i)
             {
                if (((Message)messages.elementAt(i)).getOrderId()==orderId)
                {
                    messages.removeElementAt(i);
                }
             }
        }

	public void addMessage(String from,String to,String text,String reciveDate,int orderId)
	{
            messages.addElement(new Message(from,to,text,reciveDate,orderId));
	}
	
	public int getCount()
	{
		return messages.size();
	}
	
	public String[] getShortList(int orderId)
	{
		String[] res;
                Vector temp;
                temp = new Vector();
		
		for (int i=0;i<messages.size();++i)
		{
                    if ((orderId==-1)||(((Message)messages.elementAt(i)).getOrderId()==orderId))
                    {
                        temp.addElement(((Message)messages.elementAt(i)).getReciveDate());
                    }
		}
                res = new String[temp.size()];
                for (int i=0;i<res.length;++i)
                {
                    res[i] = (String)temp.elementAt(i);
                }
		return res;
	}

        public String[] getMessage(int orderId, int ID)
        {
            String[] res=null;

            for (int i=0;i<messages.size();++i)
            if (((Message)messages.elementAt(i)).getOrderId()==orderId)
            {
                ID--;
                if (ID<0)
                {
                    res = new String[4];
                    res[0] = ((Message)messages.elementAt(i)).getFrom();
                    res[1] = ((Message)messages.elementAt(i)).getTo();
                    res[2] = ((Message)messages.elementAt(i)).getText();
                    res[3] = ((Message)messages.elementAt(i)).getReciveDate();
                    break;
                }
            }

            return res;
        }

	public String[] getMessage(int ID)
	{
		if (ID>=messages.size())
		{
			return null;
		}
		else
		{
			String[] res;
			res = new String[4];
			res[0] = ((Message)messages.elementAt(ID)).getFrom();
			res[1] = ((Message)messages.elementAt(ID)).getTo();
			res[2] = ((Message)messages.elementAt(ID)).getText();
			res[3] = ((Message)messages.elementAt(ID)).getReciveDate();
			return res;
		}
	}
}
